package com.force.jianghuling.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.force.jianghuling.R;
import com.force.jianghuling.base.BaseActivity;
import com.force.jianghuling.httputils.DataListener;
import com.force.jianghuling.httputils.HttpUtils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_login)
public class LoginActivity extends BaseActivity implements View.OnClickListener, DataListener {

    @ViewInject(R.id.et_username)
    private EditText et_username;
    @ViewInject(R.id.et_password)
    private EditText et_password;
    @ViewInject(R.id.btn_login)
    private Button btn_login;
    @ViewInject(R.id.ll_regist)
    private LinearLayout ll_regist;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }


    private void initView() {
//        initBackTitle("登录");
        btn_login.setOnClickListener(this);
        ll_regist.setOnClickListener(this);
    }

    private void login() {
        // validate
        String username = et_username.getText().toString().trim();
        if (TextUtils.isEmpty(username)) {
            showToast("请输入手机号");
            return;
        }

        String password = et_password.getText().toString().trim();
        if (TextUtils.isEmpty(password)) {
            showToast("请输入密码");
            return;
        }
        showProgressDialog();
        HttpUtils.login(context, this, HttpUtils.PUBLIC_URL + "/login", username, password);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                login();
                break;
            case R.id.ll_regist:
                startActivity(new Intent(context, RegisterActivity.class));
                finish();
                break;
        }
    }

    @Override
    public void onSuccess(String json, String type) {
        showLog("获取的登录信息：" + json);
        dismissProgressDialog();
        LoginActivity.this.finish();
        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
    }

    @Override
    public void onFail(Exception e, String type) {
        dismissProgressDialog();
        //                    showProgress(false);
        showToast(e.getMessage());
    }
}